/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.tools;

import edu.emory.mathcs.backport.java.util.Arrays;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.dicom4j.dicom.DicomException;
import org.dicom4j.dicom.DicomTags;
import org.dicom4j.io.media.DicomFile;

/**
 *
 * @author 52461
 */
public class DicomInfo {

    public static Map<String, String> parseDicom(String path) {
        DicomFile ldcm = new DicomFile(path);

        try {
            ldcm.open();
            Map<String, String> map = new HashMap<String, String>();
            map.put("pat_id", ldcm.getDataset().getElement(DicomTags.PatientID).getSingleStringValue());
            map.put("pat_name", ldcm.getDataset().getElement(DicomTags.PatientName).getSingleStringValue());
            map.put("pat_sex", ldcm.getDataset().getElement(DicomTags.PatientSex).getSingleStringValue());
//            map.put("pat_birth", ldcm.getDataset().getElement(DicomTags.PatientBirthDate).getSingleStringValue());
            map.put("modality", ldcm.getDataset().getElement(DicomTags.Modality).getSingleStringValue());
            map.put("study_iuid", ldcm.getDataset().getElement(DicomTags.StudyInstanceUID).getSingleStringValue());
            map.put("study_datetime", ldcm.getDataset().getElement(DicomTags.StudyDate).getSingleStringValue());
            try {
                map.put("pat_age", ldcm.getDataset().getElement(DicomTags.PatientAge).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }
            try {
                map.put("study_desc", ldcm.getDataset().getElement(DicomTags.StudyDescription).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }

            try {
                map.put("mvp", ldcm.getDataset().getElement(DicomTags.KVP).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }

            try {
                map.put("series_iuid", ldcm.getDataset().getElement(DicomTags.SeriesInstanceUID).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }

            try {
                map.put("series_desc", ldcm.getDataset().getElement(DicomTags.SeriesDescription).getSingleStringValue());
            } catch (Exception e) {
               // e.printStackTrace();
            }

            try {
                map.put("width", ldcm.getDataset().getElement(DicomTags.Rows).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }

            try {
                map.put("height", ldcm.getDataset().getElement(DicomTags.Columns).getSingleStringValue());
            } catch (Exception e) {
               // e.printStackTrace();
            }

            try {
                map.put("pixs", ldcm.getDataset().getElement(DicomTags.PixelSpacing).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }

            try {
                map.put("wc", ldcm.getDataset().getElement(DicomTags.WindowCenter).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }

            try {
                map.put("ww", ldcm.getDataset().getElement(DicomTags.WindowWidth).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }

            try {
                map.put("pn", ldcm.getDataset().getElement(DicomTags.ProtocolName).getSingleStringValue());
            } catch (Exception e) {
               // e.printStackTrace();
            }
            
            try {
                map.put("sop_iuid", ldcm.getDataset().getElement(DicomTags.SOPInstanceUID).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }
            
             try {
                map.put("study_device_id", ldcm.getDataset().getElement(DicomTags.StationName).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }
              try {
                map.put("instituation", ldcm.getDataset().getElement(DicomTags.InstitutionName).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }
              try {
                map.put("index_num", ldcm.getDataset().getElement(DicomTags.InstanceNumber).getSingleStringValue());
            } catch (Exception e) {
                //e.printStackTrace();
            }
               try {
                   String orient =Arrays.asList(ldcm.getDataset().getElement(DicomTags.ImageOrientation).getStringValues()).toString().replace("[", "").replace("]", "").replace(",", "\\");//0020,0037
                    String position = Arrays.asList(ldcm.getDataset().getElement(DicomTags.ImagePosition).getStringValues()).toString().replace("[", "").replace("]", "").replace(",", "\\");//0020,0032
                    String Spacing = Arrays.asList(ldcm.getDataset().getElement(DicomTags.PixelSpacing).getStringValues()).toString().replace("[", "").replace("]", "").replace(",", "\\");//0020,0032
                map.put("data1", orient+"@"+position+"@"+Spacing);
            } catch (Exception e) {
                //e.printStackTrace();
            }

            return map;
            //return ldcm.getDataset().getElement(DicomTags.SeriesInstanceUID).getSingleStringValue();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            try {
                ldcm.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (DicomException ex) {
                ex.printStackTrace();
            }
        }
    }
}
